// GameComponentManager.cs
//

using System;
using System.Collections.Generic;

namespace Moonbeam.Core
{
    public class GameComponentManager
    {
        #region singleton patten bits

        private static GameComponentManager _instance;

        private GameComponentManager() { }

        public static GameComponentManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new GameComponentManager();
                }

                return _instance;
            }
        }

        #endregion

        List<object> _components = new List<object>(1);

        public void AddComponent(object component)
        {
            //check if component type already in list
            if (ComponentTypeRegistered(component.GetType()))
            {
                throw new Exception("Game Component of Type " + component.GetType() + " is already registered.");
            }

            _components.Add(component);
        }

        public void RemoveComponent(object component)
        {
            //check if component type already in list
            if (ComponentTypeRegistered(component.GetType()))
            {
                throw new Exception("Game Component of Type " + component.GetType() + " is not registered.");
            }

            _components.Remove(component);
        }

        public bool ComponentTypeRegistered(Type type)
        {
            foreach (object currentComponent in _components)
            {
                if (currentComponent.GetType() == type)
                {
                    return true;
                }
            }

            return false;
        }

        public object GetComponent(Type type)
        {
            foreach (object currentComponent in _components)
            {
                if (currentComponent.GetType() == type)
                {
                    return currentComponent;
                }
            }

            throw new Exception("Game Component of Type " + type + " is not registered.");
        }
    }
}
